// pages/SCM/statistic/battery/battery.js
const app = getApp()
const util = require('../../../../utils/util.js')
const lbbService = require('../../../service/service.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageNum:1,
    pageSize:50,
    selectorArray: [0,0,0],
    statIndex:0,
    filterArr:['全部','更换完成','待更换'],//filterArr是statArr的label的集合，用于selector组件中的picker-view标签
    statArr:[{value:2,label:'全部'},{value:0,label:'更换完成'},{value:1,label:'待更换'}],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      scrollHeight: app.getScrollHeight(108)
    })
    this.getCitys();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /**
   * 获取省市区
   */
  getCitys: function(){
    let that = this;
    lbbService.getCitys({}, function (res) {
      that.setData({
        propArray: res
      })
      that.getHospitalList();
    })
    
  },
  /**
   * 用省市区获取医院列表
   */
  getHospitalList:function(){
    let that = this;
    let selectorArray = that.data.selectorArray;
    that.data.searchType = 'filterSearch';

    lbbService.getPileBatteryReplaceInfo({
      array: selectorArray,
      stat:that.data.statArr[that.data.statIndex].value,
      pageNum:that.data.pageNum,
      pageSize:that.data.pageSize
    }, function (res) {
      let resData = res.data;
      let hospitalList = that.data.hospitalList;
      let newArr = new Array();
      newArr = resData.concat([]);
      if (that.data.pageNum == 1) {
        hospitalList = resData;
      } else {//当下次加载的桩位与上次加载的桩位医院相同 ，则合并
        let lastHospitalPile = hospitalList[hospitalList.length - 1]
        let firstHospitlalPile = newArr.shift()
        if (lastHospitalPile && firstHospitlalPile && lastHospitalPile.hospitalName == firstHospitlalPile.hospitalName) {
          lastHospitalPile.pileBatteryInfoList = lastHospitalPile.pileBatteryInfoList.concat(firstHospitlalPile.pileBatteryInfoList)
        } else if (firstHospitlalPile) {
          newArr.unshift(firstHospitlalPile)
        } 
        hospitalList.splice(hospitalList.length - 1, 1, lastHospitalPile)
        hospitalList = that.data.hospitalList.concat(newArr)
      }
      hospitalList.forEach(item=>{
        item.totalReplaceCount = 0;
        item.totalNotReplaceCount = 0;
        item.pileBatteryInfoList.forEach(locationItem=>{
          item.totalReplaceCount += locationItem.replaceCount;
          item.totalNotReplaceCount +=  locationItem.notReplaceCount;
        })
      })
      that.setData({
        hospitalList: hospitalList,
      })
    })
  },
  /**
   * 上拉加载更多
   */
  getMoreHos(){
    let that = this;
    that.data.pageNum++;
    if(that.data.searchType=='keySearch'){
      // that.searchByKey();
    }else {
      that.getHospitalList();
    }
  },
  /**
   * 省市区筛选
   */
  myEventListener: function (e) {
    let that = this;
    that.setData({
      selectorArray: e.detail
    })
    that.data.pageNum = 1;
    that.getHospitalList()
  },
  /**
   * 触发关键词搜索时间
   */
  searchEventListener: function(e){
    let that = this;
    that.data.inputName = e.detail;
    that.setData({
      statIndex: 0
    })
    that.data.pageNum = 1;
    that.searchByKey();
  },
  /**
   * 关键词搜索请求
   */
  searchByKey: function(){
    let that = this;
    that.data.searchType = 'keySearch';
    lbbService.getPileBatteryReplaceInfoBykey({ key: that.data.inputName }, function(res){
      let resData = res.data;
      // let resData = that.data.pageNum==1?res.data:that.data.hospitalList.concat(res.data);//关键词查询没有分页
      resData.forEach(item=>{
        item.totalReplaceCount = 0;
        item.totalNotReplaceCount = 0;
        item.pileBatteryInfoList.forEach(locationItem=>{
          item.totalReplaceCount += locationItem.replaceCount;
          item.totalNotReplaceCount +=  locationItem.notReplaceCount;
        })
      })
      that.setData({
        hospitalList: resData,
      })
    })
  },
  /**
   * 筛选状态：全部/更换完成/待更换
   */
  filterListener: function (e) {
    let that = this;
    that.setData({
      statIndex: e.detail.filterIndex,
      selectorArray: e.detail.idArray,
    })
    that.data.pageNum = 1;
    that.getHospitalList();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
})